package com.example.demo.controller;

import com.example.demo.commom.AjaxResult;
import com.example.demo.commom.JwtUtils;
import com.example.demo.commom.check.ArtControllerProcessor;
import com.example.demo.commom.timer.MyTimer;
import com.example.demo.model.dto.art.ArtListDto;
import com.example.demo.model.dto.art.ArticlePubDto;
import com.example.demo.model.dto.art.OpArtDto;
//import com.example.demo.model.vo.ArtDataVO;
import com.example.demo.model.vo.ArtinfoVO;
import com.example.demo.model.vo.UserinfoVO;
import com.example.demo.service.ArtService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2024-05-14
 * Time:9:03
 */
@RestController
@RequestMapping("/album")
public class ArtController {
    @Autowired
    private ArtService artService;
    @Autowired
    private UserService userService;
    //发布专辑
    @PostMapping("/pub")
    public AjaxResult pub(HttpServletRequest request, @RequestBody ArticlePubDto dto) throws InterruptedException {
        //判断参数是否为空
        if(ArtControllerProcessor.ckpub(dto)){
            return AjaxResult.fail("参数有误");
        }
        //获取token及id
        int userId = Integer.parseInt(JwtUtils.getTokenId(String.valueOf(request.getAttribute("token"))));
//        int userId = JwtUtils.getTokenId(request.getHeader("token"));
        long now = System.currentTimeMillis();
        if(dto.getPubTime() <= now){
            //立即发布
            int res = artService.pub(dto, userId);
            if(res == -1){
                return AjaxResult.fail();
            }
        } else {
            //定时发布
            MyTimer myTimer = new MyTimer();
            myTimer.schedule(new Runnable() {
                @Override
                public void run() {
                    artService.pub(dto, userId);
                }
            },dto.getPubTime());
        }
        return AjaxResult.success("发布成功");
    }
    //查看专辑详情 - 返回值（专辑详情 + 作者详情 + 点赞收藏评论数）
//    @GetMapping("/album")
    public AjaxResult album(@RequestParam Integer id){
        if(id == null || id <= 0){
            return AjaxResult.fail(1000);
        }
        //1、专辑详情
        ArtinfoVO artinfoVO = artService.artInfo(id);
        if(artinfoVO == null){
            return AjaxResult.fail("查询异常 或 文章不存在");
        }
        //2、作者信息
        UserinfoVO userinfo = userService.getUserByid(artinfoVO.getUserId());
        //3、三连数据
        HashMap<String, Object> statRes = artService.artStat(id);
        Map<String, Object> map = new HashMap<>();
        map.put("art", artinfoVO);
        map.put("user", userinfo);
        map.put("stat", statRes);
//        ArtDataVO artDataVO = new ArtDataVO(userinfo, artinfoVO);
        return AjaxResult.success(String.valueOf(map));
    }
    //查询专辑列表
    @GetMapping("/list")
    public AjaxResult artList(
            HttpServletRequest request,
            @RequestParam(required = false) Integer start,
            @RequestParam(required = false) Integer limit,
            @RequestParam(required = false, name = "user_id") Integer userId,
            @RequestParam Integer type
    ){
        ArtListDto artListDto = new ArtListDto(start, limit, userId, type);
        //参数校验及补充
        if(ArtControllerProcessor.cklist(artListDto)){
            return AjaxResult.fail("参数有误");
        }
        //获取token及id
        int loginUserId = JwtUtils.getUserIdByRequest(request);
//        int loginUserId = JwtUtils.getTokenId(request.getHeader("token"));
//        Integer loginUserId = 2;
        Map<Integer, List<ArtinfoVO>> res = artService.list(artListDto, loginUserId);
        if(res.isEmpty() || res.get(-1) != null){
            return AjaxResult.fail("参数有误");
        }
        if(res.get(0) != null){
            return AjaxResult.success("暂无更多数据");
        }
        return AjaxResult.success(String.valueOf(res.get(1)));
    }

    @GetMapping("/del")
    public AjaxResult delArt(HttpServletRequest request, @RequestParam Integer id){
        if(id == null || id <= 0){
            return AjaxResult.fail("参数有误");
        }
        //获取token中id
        Integer userId = JwtUtils.getUserIdByRequest(request);
        Integer res = artService.del(userId, id);
        if(res != 1){
            return AjaxResult.fail(res);
        }
        return AjaxResult.success("ok");
    }

    @PostMapping("/like")
    public AjaxResult like(HttpServletRequest request, @RequestBody OpArtDto dto){
        if(dto == null || dto.getId() == null || dto.getId() <= 0){
            return AjaxResult.fail(1000);
        }
        Integer userId = JwtUtils.getUserIdByRequest(request);
        Integer res = artService.like(userId, dto.getId());
        if(res != 1 && res != -1){
            return AjaxResult.fail(1000);
        }
        return AjaxResult.success(String.valueOf(res));
    }
    @PostMapping("/collect")
    public AjaxResult collect(HttpServletRequest request, @RequestBody OpArtDto dto){
        if(dto == null || dto.getId() == null || dto.getId() <= 0){
            return AjaxResult.fail(1000);
        }
        Integer userId = JwtUtils.getUserIdByRequest(request);
        Integer res = artService.collect(userId, dto.getId());
        if(res != 1 && res != -1){
            return AjaxResult.fail(1000);
        }
        return AjaxResult.success(String.valueOf(res));
    }
}
